from setux.logger import silent
from setux.core.deploy import Deployer


class Downloader(Deployer):
    @property
    def label(self):
        return f'<- {self.name}'

    def check(self):
        dst = self.target.file.fetch(self.dst)
        return dst.size and dst.size > 0

    def deploy(self):
        return self.target.download(
            url = self.url,
            dst = self.dst,
        )


class Sender(Deployer):
    @property
    def labeler(self):
        return silent

    @property
    def label(self):
        dst = f' -> {self.dst}' if self.dst != self.src else ''
        return f'send {self.src}{dst}'

    def check(self):
        lhash = self.local.file(self.src, verbose=False).hash
        rhash = self.target.file(self.dst, verbose=False).hash
        return rhash == lhash

    def deploy(self):
        return self.target.do_send(self.src, self.dst)


class Syncer(Deployer):
    @property
    def label(self):
        dst = self.dst if hasattr(self, 'dst') else self.src
        dst = f' -> {dst}' if dst != self.src else ''
        return f'sync {self.src}{dst}'

    def check(self):
        dst = self.dst if hasattr(self, 'dst') else self.src
        lhash = self.local.dir(self.src, verbose=False).hash
        rhash = self.target.dir(dst, verbose=False).hash
        return rhash == lhash

    def deploy(self):
        dst = self.dst if hasattr(self, 'dst') else self.src
        return self.target.do_sync(self.src, dst)
